<extend name="Public/base" />
<block name="body">
    <form action="{:U('ActMoreBuy/insert')}" method="POST" id="form1" class="row" style="margin-left: 0px;margin-top:5px; ">
        <div class="col-md-12" style="line-height:50px;color:#FFF;">
            <span class="col-md-1" style="background: #353535;">商品编号</span>
            <span class="col-md-4" style="background: #353535;">商品名</span>
            <span class="col-md-1" style="background: #353535;">原价</span>
            <span class="col-md-1" style="background: #353535;">优惠价1</span>
            <span class="col-md-1" style="background: #353535;">≥份</span>
            <span class="col-md-1" style="background: #353535;">优惠价2</span>
            <span class="col-md-1" style="background: #353535;">≥份</span>
            <span class="col-md-1" style="background: #353535;">操作</span>
        </div>
        <volist name="actGoods" id="actGood">
            <div class="col-md-12" style="line-height:50px;margin-top: 3px;background: none;border: none;">
                <input type="hidden" name="pid[]" value="{$actGood.id}">
                <input type="hidden" name="pname[]" value="{$actGood.pame}">
                <input type="hidden" name="old[]" value="1">
                <span class="col-md-1 pid" data-pid="{$actGood.id}" style="border: 1px solid #30333F;">{$actGood.id}</span>
                <span class="col-md-4" style="border: 1px solid #30333F;font-weight: 700;">{$actGood.pname}{$actGood.quantity}</span>
                <span class="col-md-1" style="border: 1px solid #30333F;">{$actGood.price}</span>
                <input type="number" class="col-md-1" name="price1[]" autocomplete="off" style="border: 1px solid #30333F;color:#E83737;font-weight: 700;font-size: 20px;" value="{$actGood.price1}">
                <input type="number" class="col-md-1" name="num1[]" autocomplete="off" style="border: 1px solid #30333F;font-weight: 700;" value="{$actGood.num1}">
                <input type="number" class="col-md-1" name="price2[]" autocomplete="off" style="border: 1px solid #30333F;color:red;font-weight: 700;font-size: 20px;" value="{$actGood.price2}">
                <input type="number" class="col-md-1" name="num2[]" autocomplete="off" style="border: 1px solid #30333F;font-weight: 700;" value="{$actGood.num2}">
                <span class="col-md-1" style="border: 1px solid #30333F;font-weight: 700;"><a href="{:U('ActMoreBuy/deleteById')}&id={$actGood.id}" class="btn btn-default ajax-get confirm" style="background:#F0AD4E;color:#FFF;font-weight: 700;">删除</a></span>
            </div>
        </volist>
        <div class="col-md-12 " id="flag" style="margin-top:10px; ">
            <span class="col-md-1"><button type="button" style="margin-left: 15px;" class=" btn btn-primary" data-toggle="modal" data-target=".scorSelect">添加商品+</button></span>
            <span class="col-md-2 " ></span>
            <span class="col-md-2 btn btn-primary" id="submitForm" >确认提交</span>
            <span class="col-md-2"></span>
            <span class="col-md-4"></span>
        </div>
    </form>


    <div class="modal fade bs-example-modal-sm scorSelect" style="margin-top: 150px;" tabindex="" role="dialog" aria-labelledby="mySmallModalLabel">
        <div class="modal-dialog modal-md">
            <div class="modal-content text-center">
                <div class="modal-header text-center" style="color: #FFF;font-weight: 700;font-weight: 700;font-size: large;background: #008402">请选择商品</div>
                <div class="modal-body text-center">
                    <select id="states" style="width:100%;" class="scor-gr-color">
                        <option value="" selected></option>
                        <volist name="goodsList" id="list">
                            <option value="{$list.id}" class="scor-gr-color" data-pname="{$list.name}" data-price="{$list.price}">{$list.name}{$list.quantity}</option>
                        </volist>
                    </select>
                </div>
                <div class="modal-footer text-center">
                    <button type="button" class="btn btn-primary " style="color:#FFF;font-weight: 500" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
</block>
<block name="script">
    <script>
        $("#states").select2({
            placeholder: "请选择商品"
        });

        var $eventSelect = $("#states");
        var flag = $("#flag");
        var $pid = $(".pid");
        //商品是否已加入活动flag------数组
        var goodsId=[];
        //将数据库活动商品加入flag
        $pid.each(function(){
            var $this = $(this);
            goodsId.push(String($this.data('pid')));
        });
        $eventSelect.on("change", function (e) {
             //判断新加商品是否已在flag中，>-1为存在
            if(goodsId.indexOf($eventSelect.val())>-1){
                return false;
            }else{
                goodsId.push($eventSelect.val());
                flag.before(
                        '<div class="col-md-12"  id="'+$eventSelect.val()+'" style="line-height:50px;margin-top: 3px;">'+
                        '<input type="text" readonly style="border:1px solid #428BCA;" class="col-md-1 pid" data-pid="'+$eventSelect.val()+'"  name="pid[]"   value="'+$eventSelect.val()+'" >' +
                        '<input type="text" readonly style="border:1px solid #428BCA;" class="col-md-4 " name="pname[]" value="'+$("#states  option:selected").data('pname')+'" >' +
                        '<input type="text"  readonly style="border:1px solid #428BCA;" class="col-md-1"  name="price[]" value="'+$("#states  option:selected").data('price')+'">' +
                        '<input type="number" autocomplete="off" style="border:1px solid #428BCA;" class="col-md-1" name="price1[]">'+
                        '<input type="hidden" name="old[]" value="0">'+
                        '<input type="number" autocomplete="off" style="border:1px solid #428BCA;" class="col-md-1"  name="num1[]">'+
                        '<input type="number" autocomplete="off" style="border:1px solid #428BCA;" class="col-md-1"  name="price2[]">'+
                        '<input type="number" autocomplete="off" style="border:1px solid #428BCA;" class="col-md-1"  name="num2[]">'+
                        '<span class="col-md-1" style="border: 1px solid #30333F;font-weight: 700;" onclick="deleteThis('+$eventSelect.val()+')"><a  class="btn btn-default ajax-get confirm" style="background:#F0AD4E;color:#FFF;font-weight: 700;">删除</a></span>' +
                        '<span class="col-md-3"></span>' +
                        ' </div>');
            }
        }
        );
        $("#submitForm").click(function(){
            $("#form1").submit();
        });
        function deleteThis(id){
            id=String(id);
            goodsId.splice(goodsId.indexOf(id),1);
            $("#"+id).remove();
        };
    </script>
</block>
